package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.ListSelectionModel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JButton;

import controllers.ClientController;
import controllers.DBConnection;

import java.awt.event.ActionListener;

import javax.swing.JScrollPane;

import entity.User;
import messages.SearchPicMessage;

public class ShowCatalog extends JFrame {
	ClientController clientCon;
	private JPanel contentPane;
	static String place=null;
	 String place1;
	 String time;

	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private String Place ; 
	private String Time;
	static String ID;
	private String PicID;
	private String finaly;
	private String picIdentety;
	public String url;
	public String url1;
	int picIDD;
	String CusID;
	String ss;
	double bill;
	public String price;
	
	
	
	private JList<String> list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ShowCatalog frame = new ShowCatalog(place,ID);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public ShowCatalog(String Place, String id)  {
		this.ID=id;
		setTitle("Catalog of Pictures");
		 place=Place;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		

		list = new JList<String>();
		list.setBounds(30, 61, 168, 154);
		list.setVisibleRowCount(30);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(null);
		contentPane.add(list);
		list.addComponentListener(new ComponentAdapter() {
			
			public void componentMoved(ComponentEvent arg0) {
			}
		});
		
		try
			{
			
				DefaultListModel<String> model = new DefaultListModel<String>();
				DBConnection conn1=new DBConnection();
				java.sql.PreparedStatement pr9= conn1.getDBConnection().prepareStatement("SELECT picPlace,picTime FROM sgi_system.archive WHERE description=? ");
				pr9.setString(1, place);
				ResultSet rs9 = pr9.executeQuery();
				while(rs9.next()){
					place1=rs9.getString("picPlace");
					System.out.println("here");
					
					model.addElement(place1);
				}
				System.out.println(model);
				list.setModel(model);
				
				
				
			}//try
			catch (Exception e){e.printStackTrace();}
		
		
		
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				
	
			}
		});
		
		lblNewLabel = new JLabel("Catalog of "+place);
		lblNewLabel.setFont(new Font("Tahoma", Font.ITALIC, 16));
		lblNewLabel.setBounds(216, 0, 194, 39);
		contentPane.add(lblNewLabel);
		
		lblNewLabel_1 = new JLabel("icon");
		lblNewLabel_1.setIcon(new ImageIcon(place+"//icon.jpg"));
		lblNewLabel_1.setBounds(343, 43, 211, 237);
		contentPane.add(lblNewLabel_1);
		
		btnNewButton = new JButton("Buy&View  Picture");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				System.out.println("cus Id"+ID);
				int len;
				byte[] fileBytes;
			
		
				try{
						/**
						 *  sql connection
						 */
						 
						DBConnection conn=new DBConnection();
						java.sql.PreparedStatement pr1= conn.getDBConnection().prepareStatement("SELECT picID,picPrice " + "FROM sgi_system.archive " + "WHERE  picPlace =? ");
						pr1.setString(1,list.getSelectedValue());
						//pr1.setString(2,picturearr[2]);
						ResultSet ppp=pr1.executeQuery();

						
						/**
						 * if no such pic in DB
						 */
						if (!ppp.next() ) {
						    System.out.println("No image in the DB");
						    JOptionPane.showMessageDialog(null, "No image in the DB ");
						    return;
						 
						} else {
							/**
							 * url of the specific picture handle
							 */
						    do {
						    	price=ppp.getString("picPrice");
						    	 picIDD=ppp.getInt(1);
								 PicID=Integer.toString(picIDD);
								 String s1=".jpg";
								 String s2="images/";
								  finaly=s2+PicID+s1;
								  picIdentety= PicID+s1;
								System.out.println("->>>>Wanted Picture Id:\t"+PicID);
						    } while (ppp.next());
						}
						
						
							
						
							
					
								
				}//try
					catch (Exception e1){e1.printStackTrace();}
		 
		 
		
				try
					{
							/**
							 * get image
							 */
						DBConnection conn1=new DBConnection();
						java.sql.PreparedStatement pr8= conn1.getDBConnection().prepareStatement("SELECT * FROM sgi_system.image WHERE picID=? ;");
						pr8.setString(1,picIdentety);
						ResultSet rs = pr8.executeQuery();
						
						
						
						int selectedOption=JOptionPane.showConfirmDialog(null, "Do you want to buy this picture", "Buy picture", JOptionPane.YES_NO_OPTION);/*confirm buying picture*/
						if (selectedOption == JOptionPane.YES_OPTION) {
						
						   /*check if he bought the picture already*/
							java.sql.PreparedStatement pr1 = conn1.getDBConnection().prepareStatement("SELECT picID FROM sgi_system.purchasespic WHERE customerID=?;");
							pr1.setString(1, ID);
							ResultSet rs1=pr1.executeQuery();
							
							while(rs1.next()){
								
								ss=rs1.getString("picID");
								if(Integer.valueOf(ss)==picIDD){
									
									JOptionPane.showMessageDialog(null, "You already bought this picture");
									return;
								}
								
								java.sql.PreparedStatement pr10 = conn1.getDBConnection().prepareStatement("SELECT bill FROM sgi_system.customer WHERE customerID=?;");
								pr10.setString(1, ID);
								ResultSet rs10=pr10.executeQuery();
								while(rs10.next()){
									bill=rs10.getDouble("bill");
									bill=bill+Double.valueOf(price);
									System.out.println(bill);
								}
								
								
								java.sql.PreparedStatement pr11 = conn1.getDBConnection().prepareStatement("UPDATE   sgi_system.customer  SET bill=?    WHERE customerID=?;");
								pr11.setString(2, ID);
								pr11.setDouble(1, bill);
								pr11.executeUpdate();
								
							}
							
							
	
							
							java.sql.PreparedStatement pr2 = conn1.getDBConnection().prepareStatement("INSERT INTO sgi_system.purchasespic (customerID,picID) VALUES(?,?);");
						
							pr2.setInt(1,Integer.valueOf(ID) );
							pr2.setInt(2, picIDD);			
							pr2.executeUpdate();
						while(rs.next())
							{
				                fileBytes = rs.getBytes(3);
				                url=("C://picc//image//");
				                url1=picIdentety;
				                OutputStream targetFile=new FileOutputStream(url+url1);
				                targetFile.write(fileBytes);
				                targetFile.close();
							}//while
						}
					
						else return;
					}//try
					catch (Exception e2){e2.printStackTrace();}

					/**
					 * open requested picture with zoom MapScale class
					 */
			
					 MapScale test = new MapScale(url+url1);
					 try {
						test.Run();
					} catch (IOException e1) {e1.printStackTrace();}
			}
		});
		btnNewButton.setBounds(28, 229, 170, 23);
		contentPane.add(btnNewButton);
		
		btnNewButton_1 = new JButton("Back");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WMap frame = new WMap(ID);
				frame.setVisible(true);
				dispose();
			}
		});
		btnNewButton_1.setBounds(28, 263, 170, 23);
		contentPane.add(btnNewButton_1);
		
	
		
		
		
		
		
		
		
		
	}
}
